package codeRandomThoughts.Test101对称二叉树;

import codeRandomThoughts.utils.TreeNode;

//二刷
public class Solution3 {
    public boolean isSymmetric(TreeNode root) {
        return isSym(root.left, root.right);
    }

    //这题目递归的参数必须是两个,不然写不了
    public boolean isSym(TreeNode left, TreeNode right) {
        //终止条件
        if (left == null && right == null) {
            return true;
        } else if (left == null || right == null) {
            return false;
        } else if (left.val != right.val) {
            return false;
        }

        //剩下一种情况,就是左右子树不为空,并且值相同
        //此时要想对称,必须左子树的右子树和右子树的左子树对称
        //左子树的左子树和右子树的右子树对称
        boolean tem1 = isSym(left.left, right.right);
        boolean tem2 = isSym(left.right, right.left);
        return tem1 && tem2;
    }

}
